package com.study.methodcontrucet;

import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @program: java8
 * @description: 构造器引用
 * 和方法引用类似，函数式接口的抽象方法的形参列表和构造器的形参列表一致。
 * 抽象方法的返回值类型即为构造器所属的类的类型
 * @author: yangyb
 * @create: 2021-11-08 22:46
 **/
public class ConstructorRefTest {
    /**
     * @Description: 构造器引用
     * supplier 中的T get()
     * Employee 的空参构造器：Employee()
     * @Param: []
     * @return:
     */
    @Test
    public void test1() {
        Supplier<Employee> supplier = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println("****************");
        Supplier<Employee> supplier1 = () -> new Employee();
        System.out.println(supplier1.get());

        Supplier<Employee> supplier2 = Employee::new;
        System.out.println(supplier2.get());

    }

    /**
     * @Description: Function 中的R apply(T t)
     * @Param: []
     * @return:
     */
    @Test
    public void test2() {
        Function<Integer, Employee> employeeFunction = id -> new Employee(id);
        Employee employee = employeeFunction.apply(1003);
        System.out.println(employee);

        System.out.println("************");

        Function<Integer, Employee> employeeFunction1 = Employee::new;
        Employee employee1 = employeeFunction1.apply(1003);
        System.out.println(employee1);

    }

    /**
     * @Description: BiFunction 中的R apply(T t,U u)
     * @Param: []
     * @return:
     */
    @Test
    public void test3() {
        BiFunction<Integer, String, Employee> biFunction = (id, name) -> new Employee(id, name);
        Employee employee = biFunction.apply(1004, "张三");
        System.out.println(employee);

        System.out.println("**************************");
        BiFunction<Integer, String, Employee> biFunction1 = Employee::new;
        Employee employee1 = biFunction1.apply(12222, "lisi");
        System.out.println(employee1);


    }

    /**
     * @Description: 数组的引用
     * Function 中的R apply(T t)
     * @Param: []
     * @return:
     */
    @Test
    public void test4() {
        Function<Integer, String[]> function = length -> new String[length];
        String[] strings = function.apply(5);
        System.out.println(Arrays.toString(strings));
        System.out.println("****************");

        Function<Integer, String[]> function1 = String[]::new;
        String[] strings2 = function1.apply(5);
        System.out.println(Arrays.toString(strings2));


    }
}
